<% layout('/layouts/default.html', {title: '资方汽车供需合同管理', libs: ['validate','fileupload','dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(investorCarContract.isNewRecord ? '新增资方汽车供需合同' : '编辑资方汽车供需合同')}
			</div>
			<div class="box-tools pull-right">
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${investorCarContract}" action="${ctx}/settlement/investorCarContract/save" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<#form:hidden path="investorCarContractId"/>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('单据编号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="investorCarContractCode" readonly="true" placeholder="自动生成" maxlength="100" class="form-control"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('资方')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
                                <#form:select path="investorId" items="${listSupplier}" id="investorId"
                                itemLabel="supplierName" itemValue="supplierId" blankOption="true" class="form-control searchCar required" />
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('代理公司')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
                                <#form:treeselect id="companyId" title="${text('公司选择')}" id="companyId"
                                path="companyId.companyCode" labelPath="companyId.companyName"
                                url="${ctx}/sys/company/treeData" canSelectRoot="true" canSelectParent="true"
                                class="required searchCar" allowClear="true" callbackFuncName="companyCallback" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('订单日期')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="date" readonly="true" maxlength="20" class="form-control laydate required"
									dataFormat="date" data-type="date" data-format="yyyy-MM-dd"/>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2" title="">
								<span class="required hide">*</span> ${text('备注')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-10">
								<#form:textarea path="remarks" rows="4" maxlength="500" class="form-control"/>
							</div>
						</div>
					</div>
				</div>
                <div class="form-unit">${text('车辆信息')}</div>
                <div class="ml10 mr10">
                    <table id="investorCarContractDetailDataGrid"></table>
                    <% if (hasPermi('settlement:investorCarContract:edit')){ %>
                    <a href="#" id="investorCarContractDetailDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> ${text('增行')}</a>
                    <% } %>
                </div>
                <div class="form-unit">${text('附件信息')}</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2">
								<span class="required hide">*</span> ${text('附件上传')}：</label>
							<div class="col-sm-10">
								<#form:fileupload id="uploadFile" bizKey="${investorCarContract.id}" bizType="investorCarContract_file"
									uploadType="all" class="" readonly="false" preview="true"/>
							</div>
						</div>
					</div>
				</div>
                <div class="form-unit">${text('系统信息')}</div>
                <div class="row">
                    <div class="col-xs-6">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required hide">*</span> ${text('创建日期')}：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <#form:input path="createDate" class="form-control" disabled="true"
                                dataFormat="datetime2" data-type="datetime" data-format="yyyy-MM-dd HH:mm:ss"/>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-6">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required hide">*</span> ${text('创建人')}：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <#form:input path="createBy" class="form-control" disabled="true"/>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-6">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required hide">*</span> ${text('最后修改日期')}：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <#form:input path="updateDate" readonly="true" maxlength="20" class="form-control"
                                dataFormat="datetime2" data-type="datetime" data-format="yyyy-MM-dd HH:mm:ss"/>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-6">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required hide">*</span> ${text('最后修改人')}：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <#form:input path="updateBy" readonly="true" maxlength="20" class="form-control"/>
                            </div>
                        </div>
                    </div>
                </div>
			</div>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
						<% if (hasPermi('settlement:investorCarContract:edit')){ %>
							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;
						<% } %>
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<% } %>
<script>
//初始化资方汽车供需合同明细DataGrid对象
$("#investorCarContractDetailDataGrid").dataGrid({
	data: ${toJson(investorCarContract.investorCarContractDetailList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度
	
	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'investorCarContractDetailId', editable:true, hidden:true},
        {header:'${text("车架号")}', name:'frameNumber', width:190,
            editable:true, edittype:'select', editoptions:{'class':'form-control required',
                items: ${toJson(listFrameNumber)},
                itemLabel: 'frameNumber', itemValue: 'frameNumber',
                dataInit: function(element){
                    $(element).select2().on("change",function(){
                        $(this).resetValid();
                        var contract =  {};
                        $.ajax({
                            type: "POST",
                            async : false,
                            url: "${ctx}/order/serviceorder/serviceOrder/getOrderDetailByFrameNumber",
                            data: {"frameNumber": $(this).val()},
                            success: function (result) {
                                contract.orderId = result.orderId;
                                contract.orderName = result.serviceOrderName;
                                contract.carId = result.carId;
                                contract.carName = result.carName;
                                contract.actualAmount = result.actualAmount;
                            }
                        });
                        $(this).closest('tr').find('[aria-describedby="investorCarContractDetailDataGrid_serviceOrderId"] .editable').val(contract.orderId).trigger('change');
                        $(this).closest('tr').find('[aria-describedby="investorCarContractDetailDataGrid_serviceOrderName"]').text(contract.orderName);
                        $(this).closest('tr').find('[aria-describedby="investorCarContractDetailDataGrid_carId"] .editable').val(contract.carId).trigger('change');
                        $(this).closest('tr').find('[aria-describedby="investorCarContractDetailDataGrid_carName"]').text(contract.carName);
                        $(this).closest('tr').find('[aria-describedby="investorCarContractDetailDataGrid_calculatePrice"] .editable').val(contract.actualAmount);
                        $(this).closest('tr').find('[aria-describedby="investorCarContractDetailDataGrid_calculatePrice"]').text(contract.actualAmount).trigger('change');
                    });
                }
            }
        },
        {header:'${text("服务订单")}', name:'serviceOrderId', editable:true, hidden:true},
        {header:'${text("服务订单")}', name:'serviceOrderName', editable:false, width:300, edittype:'text', editoptions:{'maxlength':'255', 'class':'form-control'}},
        /*{header:'${text("服务订单")}', name:'serviceOrderId', width:300, editable:true, edittype:'select', editoptions:{'maxlength':'64', 'class':'form-control',
                items: ${toJson(listServiceOrder)},
                itemLabel: 'serviceOrderCode', itemValue: 'serviceOrderId', dataInit: function(element){
                    $(element).select2().on("change",function(){
                        $(this).resetValid();
                    });
                }
        }},*/
        {header:'${text("车辆")}', name:'carId', editable:true, hidden:true},
        {header:'${text("车辆")}', name:'carName', editable:false, width:300, edittype:'text', editoptions:{'maxlength':'255', 'class':'form-control'}},
        /*{header:'${text("车辆")}', name:'carId', width:300, editable:true, edittype:'select', editoptions:{'maxlength':'64', 'class':'form-control',
                items: ${toJson(tcarList)},
                itemLabel: 'carName', itemValue: 'carId', dataInit: function(element){
                    $(element).select2().on("change",function(){
                        $(this).resetValid();
                    });
                }
        }},*/
        {header:'${text("计算车价")}', name:'calculatePrice', editable:true, hidden:true},
        {header:'${text("计算车价")}', name:'calculatePrice', width:150, editable:false, edittype:'text', editoptions:{'maxlength':'255', 'class':'form-control'}},
        {header:'${text("单价")}', name:'price', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control required number'}},
		{header:'${text("金额含税合计")}', name:'amount', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control required number'}},
		{header:'${text("发票信息")}', name:'invoice', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control required number'}},
		{header:'${text("备注")}', name:'remark', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'255', 'class':'form-control'}},
		{header:'${text("资方汽车供需合同ID")}', name:'investorCarContractId.investorCarContractId', editable:true, hidden:true},
		{header:'${text("操作")}', name:'actions', width:80, sortable:false, fixed:true, formatter: function(val, obj, row, act){
			var actions = [];
			if (val == 'new'){
				actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#investorCarContractDetailDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}else{
				actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#investorCarContractDetailDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}
			return actions.join('');
		}, editoptions: {defaultValue: 'new'}}
	],
	
	// 编辑表格参数
	editGrid: true,				// 是否是编辑表格
	editGridInitRowNum: 0,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#investorCarContractDetailDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowInitData: {investorCarContractDetailId: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据
	
	// 编辑表格的提交数据参数
	editGridInputFormListName: 'investorCarContractDetailList', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,investorCarContractDetailId,frameNumber,serviceOrderId,carId,calculatePrice,price,amount,remark,investorCarContractId.investorCarContractId,invoice', // 提交数据列表的属性字段
	
	// 加载成功后执行事件
	ajaxSuccess: function(data){
		
	}
});
</script>
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($(form), function(data){
			js.showMessage(data.message);
			if(data.result == Global.TRUE){
				js.closeCurrentTabPage(function(contentWindow){
					contentWindow.page();
				});
			}
		}, "json");
    }
});

$(".searchCar").change(function () {
    var investorId = $("#investorId").val();
    var companyId = $("#companyIdCode").val();
    if (companyId && investorId){
        var url = "${ctx}/order/serviceorder/serviceOrder/getFrameNumberByCondition?investorId="+investorId+"&companyId="+companyId;
        $.post(url,null,function (result) {
            var str = ''
            for (var i = 0; i < result.length; i++) {
                var frameNumber1 = result[i].frameNumber;
                if (i != result.length - 1) {
                    str += frameNumber1 + ":" + frameNumber1 + ";";
                } else {
                    str += frameNumber1 + ":" + frameNumber1;
                }
            }

            $('#investorCarContractDetailDataGrid').setColProp('frameNumber', {editoptions: {value:str,readOnly: true}});
            $('#investorCarContractDetailDataGrid').dataGrid('reloadGrid');
        });
    }else{
        // $("#frameNumber").empty();
        var str = '';
        $('#investorCarContractDetailDataGrid').setColProp('frameNumber', {editoptions: {value:str,readOnly: true}});
        $('#investorCarContractDetailDataGrid').dataGrid('reloadGrid');
    }
});
</script>